import 'package:flutter/cupertino.dart';
import 'package:flutter_09_game/base/controller/base_controller.dart';
import 'package:flutter_09_game/http/http_export.dart';
import 'package:flutter_09_game/pages/profile/models/model_security_question.dart';
import 'package:get/get.dart';
class ProfileFindLoginPasswordLogic extends BaseController {
  // TextEditingController oldPasswordController = TextEditingController();
  TextEditingController newPasswordController = TextEditingController();
  TextEditingController newConfirmPasswordController = TextEditingController();

  // RxString newFundPassword = ''.obs;
  // RxString newFundPasswordConfirm = ''.obs;
  TextEditingController answerController = TextEditingController();

  RxBool isFormValid = false.obs;
  Rx<ModelSecurityQuestion> securityQuestion = Rx(ModelSecurityQuestion());

  // answer

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    getUserSecurityQuestion();
    // everAll([ newFundPassword, newFundPasswordConfirm], (val) {
    //   validateForm();
    // });
    answerController.addListener(validateForm);
    // oldPasswordController.addListener(validateForm);
    newPasswordController.addListener(validateForm);
    newConfirmPasswordController.addListener(validateForm);
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    answerController.dispose();
    // oldPasswordController.dispose();
    newPasswordController.dispose();
    newConfirmPasswordController.dispose();
  }

  getUserSecurityQuestion() async {
    HttpResponse response = await HttpManager().get(API.getUserSecurityQuestion,
        fromJson: ModelSecurityQuestion.fromJson);
    if (response.isSuccess) {
      securityQuestion.value = response.data;
    }
  }

  /// 表单验证
  void validateForm() {
    isFormValid.value = answerController.text.trim().isNotEmpty &&
        // oldPasswordController.text.trim().length > 5 &&
        newPasswordController.text.trim().length > 5 &&
        newConfirmPasswordController.text.trim().length > 5;
  }

  postUserFindPassword() async {
    HttpResponse response = await HttpManager().post(API.postUserFindPassword,
        params: {
          'answer': answerController.text.trim(),
          // 'old_password': oldPasswordController.text.trim(),
          'new_password': newPasswordController.text.trim(),
          'new_password_confirm': newConfirmPasswordController.text.trim(),
        },
        uiType: HttpUIType.loadingAndToast);
    if (response.isSuccess) {
      Get.back(result: true);

      // LoadingUtils.showToast(LocaleKeys.operationSuccess.tr);
      // AppUserManager().refreshUserInfo();
    }
  }
}
